<template>
  <div class="singer">
     <list-view :data="singerDataList" @goSingerDetail="goSingerDetail"></list-view>
     <router-view></router-view>
  </div>
</template>

<script>
import { getSingerList } from '@/api/singer'
import { ERR_OK } from '@/api/config'
import Singer from '@/common/js/singer'
import ListView from '@/base/listview/listview'
import {mapMutations} from 'vuex'
const HOT_NAME = '热门'
const HOT_LEN = 10
export default {
  components: {
    ListView
  },
  props: {},
  data() {
    return {
      singerDataList: []
    }
  },
  watch: {},
  computed: {},
  methods: {
    ...mapMutations({
      setSinger: 'SET_SINGER'
    }),
    goSingerDetail(item) {
      console.log(item)
      this.setSinger(item)
      this.$router.push({
        path: `/singer/${item.id}`
      })
    },
    _getSingerList() {
      getSingerList().then((res) => {
        if (res.code === ERR_OK) {
          this.singerDataList = this._normalizeSinger(res.data.list)
        }
      })
    },
    _normalizeSinger(list) {
      let map = {
        hot: {
          title: HOT_NAME,
          items: []
        }
      }
      list.forEach((element, index) => {
        if (index < HOT_LEN) {
          map.hot.items.push(new Singer({
            name: element.Fsinger_name,
            id: element.Fsinger_mid
          }))
        }
        const key = element.Findex
        if (!map[key]) {
          map[key] = {
            title: key,
            items: []
          }
        }
        map[key].items.push(new Singer({
          name: element.Fsinger_name,
          id: element.Fsinger_mid
        }))
      })
      let hot = []
      let ret = []
      for (let key in map) {
        let val = map[key]
        if (val.title === HOT_NAME) {
          hot.push(val)
        } else if (val.title.match(/[a-zA-Z]/)) {
          ret.push(val)
        }
      }
      ret.sort((a, b) => {
        return a.title.charCodeAt(0) - b.title.charCodeAt(0)
      })
      return hot.concat(ret)
    }
  },
  created() {
    this._getSingerList()
  },
  mounted() {}
}
</script>
<style lang="stylus" scoped>
.singer
  position: fixed
  top: 88px
  bottom: 0
  left: 0
  width: 100%
</style>